
const fs = require('fs');
const componentName = ['button', 'icon', 'layout', 'pagination', 'tabs', 'steps', 'breadcrumb', 'menu', 'checkbox', 'select', 'switch', 'radio', 'search', 'cascader', 'filtrate', 'inputNumber', 'input', 'slider', 'transfer', 'upload', 'timePicker', 'badge', 'collapse', 'tooltips', 'tag', 'timeline', 'table', 'progress', 'spin', 'alert', 'drawer', 'message', 'modal', 'notification', 'popconfirm']
const logs = () => {
  let logPath = ['./dataFeat.json', './dataFix.json', './dataOther.json']
  for (let p of logPath) {
    if (p.includes("Feat")) {
      extractFs(p, '_feat')
    }
    if (p.includes('Fix')) {
      extractFs(p, '_fix')
    }
    if (p.includes('Other')) {
      extractFs(p, '_other')
    }
    // if (p.includes('Null')) {
    //   extractFs(p, '_null')
    // }
  }
}

const extractFs = (p, mode) => {
  fs.readFile(p, 'utf-8', (err, data) => {
    if (!err) {
      let objs = {
        [mode]: []
      };
      const lines = data.split(/\r?\n/);
      lines.forEach((line) => {
        if (line) {
          let temp = JSON.parse(line);
          let type = temp.type
          let header = temp.header
          let hash = temp.hash.substring(0, 7)
          let jira = temp.issue ? temp.issue : "";
          let commitData = temp.committerDate
          //匹配描述中的组件名
          let arr=findComponent(header);
          let obj = {
            'type': type,
            'header': header,
            'hash': hash,
            'jira': jira,
            'commitData': commitData,
            name:arr
          }
          objs[mode].push(obj)
        }
      });
      const ws = fs.createWriteStream(`./${mode}.json`, { flags: 'w' })
      ws.write(JSON.stringify(objs, null, "\t"))
      ws.close();
      console.log(mode + ' 生成成功！！')
    } else {
      console.log(mode + " 生成失败")
    }

  })
}

const findComponent=(str)=>{
  let arr=[];
  for(let name of componentName){
    let pattern=name;
    if(name[name.length-1]==='s'){
      pattern=name.substring(0,name.length-1)
    }
    let re=new RegExp(pattern,'i')
    //
    if(re.test(str)){
      // console.log('ok',name)
      arr.push(name)
    }
  }
  return arr;
}

logs()




